import requests
import datetime

get_url = "http://consul-client:8500/v1/health/state/critical"
put_url = "http://consul-client:8500/v1/agent/service/deregister/"
date_now = datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')
try:
    response = requests.get(get_url)
    services = response.json()
    if services:
        for service in services:
            service_id = service["ServiceID"]
            put_response = requests.put(put_url+service_id)
            if put_response.status_code == 200:
                print("{} Invalid Service {} is Deleted Successful!!!".format(date_now, service_id))
            else:
                print("{} Invalid Service {} is Deleted Failed!!!".format(date_now, service_id))
    else:
        print("{} ALL Service Is Healthy!!!".format(date_now))
except Exception as e:
    print(e.args)
